package com.briup.jz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.BaseConfig;
import com.briup.jz.bean.BaseConfigExample;
import com.briup.jz.dao.BaseConfigMapper;
import com.briup.jz.service.IBaseConfigService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseConfigServiceImpl implements IBaseConfigService {
	
	@Autowired
	private BaseConfigMapper configMapper;

	@Override
	public void saveOrUpdate(BaseConfig config) throws CustomerException {
		if(config.getId()!=null) {
			configMapper.updateByPrimaryKey(config);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
			BaseConfigExample example = new BaseConfigExample();
			example.createCriteria().andNameEqualTo(config.getName());
			List<BaseConfig> list = configMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
			}
			configMapper.insert(config);
		}
	}

	@Override
	public List<BaseConfig> query(Long id) {
		BaseConfigExample example = new BaseConfigExample();
		if(id!=null){
			example.createCriteria().andIdEqualTo(id);
		}
		List<BaseConfig> list = configMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseConfig config = configMapper.selectByPrimaryKey(id);
		if(config == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		configMapper.deleteByPrimaryKey(id);
		
	}
}
